package server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;

/**
 * @file Resource.java
 * @version 0.2
 * @author James Dibble 10009689
 */
public final class Resource {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private static int intUploadCount;
    private String resourceName, resourceDescription, resourceFileName;
    private int resourceID;
    private File file;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    public Resource(int resourceID, String resourceFileName, String resourceName, String resourceDescription) {
        this.resourceName = resourceName;
        this.resourceID = resourceID;
        this.resourceFileName = resourceFileName;
        this.resourceDescription = resourceDescription;
        this.file = new File(getFilePath());
        intUploadCount = 0;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    public synchronized void UploadResourceAsString() {
        int tmp = intUploadCount;
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            System.out.println("sleep interrupted");
        }
        intUploadCount = tmp + 1;
        System.out.println(String.format("%s has been downloaded %s time(s)", 
                                         this.resourceName,
                                         intUploadCount));
    }
    
    public synchronized void UploadResourceAsFile() throws SocketException, FileNotFoundException, IOException{
        int tmp = intUploadCount;
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            System.out.println("sleep interrupted");
        }
        intUploadCount = tmp + 1;
        System.out.println(String.format("%s has been downloaded %s time(s)", 
                                         this.resourceName,
                                         intUploadCount));
    }
    
    public String getResourceName(){
        return this.resourceName;
    }
    
    public int getResourceID(){
        return this.resourceID;
    }
    
    public String getFilePath(){
        return getClass().getResource(this.resourceFileName).getPath();
    }
    
    public File getResourceAsFile(){
        return this.file;
    }
    
    public String getFileName(){
        return this.resourceFileName;
    }
    
    @Override
    public String toString(){
        return this.resourceDescription;
    }
    // </editor-fold>
}   
